<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<script>

function handle(list, id, event)
{
    list.push(event.type + "@" + id);
}

function testEventsFiredOn(doc)
{
    window.caughtFoo = [];
    var root = doc.documentElement;
    var target = doc.getElementById("target");

    target.addEventListener("foo", handle.bind(null, caughtFoo, "target"));
    target.setAttribute("onclick", "caughtFoo.push('click@target')");
    root.addEventListener("foo", handle.bind(null, caughtFoo, "root"));
    root.setAttribute("onclick", "caughtFoo.push('click@root')");

    window.caughtFoo.length = 0;
    target.dispatchEvent(new CustomEvent("foo", { bubbles: true }));
    shouldBe("window.caughtFoo", "['foo@target', 'foo@root']");

    window.caughtFoo.length = 0;
    target.dispatchEvent(new MouseEvent("click", { bubbles: true }));
    shouldBe("window.caughtFoo", "[]");
}

window.jsTestIsAsync = true;

var xhrForXML = new XMLHttpRequest();
xhrForXML.open("GET", "resources/hello-xhr-event.xml", false);
xhrForXML.send();
testEventsFiredOn(xhrForXML.responseXML);

// Must run asynchronous to use responseType.
var xhrForHTML = new XMLHttpRequest();
xhrForHTML.open("GET", "resources/hello-xhr-event.html", true);
xhrForHTML.responseType = "document";
xhrForHTML.onload = function() {
    testEventsFiredOn(xhrForHTML.response);
    finishJSTest();
};
xhrForHTML.send();

</script>
</body>
</html>
